package com.googlecode.c4e.xmlrpcclient;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.maven.continuum.xmlrpc.client.ContinuumXmlRpcClient;

import com.googlecode.c4e.api.model.BuildResult;
import com.googlecode.c4e.api.model.Project;
import com.googlecode.c4e.api.model.ProjectGroup;
import com.googlecode.c4e.api.model.Server;
import com.googlecode.c4e.services.ContinuumConnector;

/**
 * @author gfernandes
 */
public class ContinuumXMLRPCClientConnector implements ContinuumConnector {

	private ContinuumXmlRpcClient client;

	public void buildProject(Integer projectId) throws Exception {
		this.client.buildProject(projectId);
	}

	public Integer checkProjectState(Integer projectId) throws Exception {
		Map summaryMap = client.getProjectSummaryRPC(projectId);
		return Integer.valueOf(String.valueOf(summaryMap.get("state")));
	}

	public void connect(Server server) throws Exception {
		this.client = new ContinuumXmlRpcClient(server.buildUrl(), server.getUsername(), server.getPassword());
	}

	public List<ProjectGroup> getAllProjectGroups(Server server) throws Exception {
		List<Object> summaries = this.client.getAllProjectGroupsRPC();
		List<ProjectGroup> result = new ArrayList<ProjectGroup>(summaries.size());
		for (Object summary : summaries) {
			Map summaryMap = (Map) summary;
			ProjectGroup group = new ProjectGroup();
			group.setId(Integer.valueOf(String.valueOf(summaryMap.get("id"))));
			group.setName(String.valueOf(summaryMap.get("name")));
			group.setGroupId(String.valueOf(summaryMap.get("groupId")));
			group.setDescription(String.valueOf(summaryMap.get("description")));
			group.setServer(server);
			result.add(group);
		}
		return result;
	}

	public BuildResult getLatestBuildResult(Project project) throws Exception {
		Map resultMap = this.client.getLatestBuildResultRPC(project.getId());
		Map projectMap = (Map) resultMap.get("project");
		int buildNumber = Integer.parseInt(String.valueOf(projectMap.get("latestBuildId")));
		BuildResult result = new BuildResult();
		result.setProject(project);
		result.setBuildNumber(buildNumber);
		result.setOutput(client.getBuildOutput(project.getId(), buildNumber));
		return result;
	}

	public List<Project> getProjects(ProjectGroup projectGroup) throws Exception {
		List<Object> summaries = this.client.getProjectsRPC(projectGroup.getId());
		List<Project> result = new ArrayList<Project>(summaries.size());
		for (Object summary : summaries) {
			Map summaryMap = (Map) summary;
			Project project = new Project();
			project.setGroup(projectGroup);
			project.setName(String.valueOf(summaryMap.get("name")));
			project.setArtifactId(String.valueOf(summaryMap.get("artifactId")));
			project.setGroupId(String.valueOf(summaryMap.get("groupId")));
			project.setVersion(String.valueOf(summaryMap.get("version")));
			project.setId(Integer.valueOf(String.valueOf(summaryMap.get("id"))));
			project.setState(Integer.valueOf(String.valueOf(summaryMap.get("state"))));
			project.setScmUrl(String.valueOf(summaryMap.get("scmUrl")));
			result.add(project);
		}
		return result;
	}

	public void ping() throws Exception {
		this.client.ping();
	}

	public List<BuildResult> getBuildResults(Project project) throws Exception {
		List<Object> results = this.client.getBuildResultsForProjectRPC(project.getId());
		List<BuildResult> result = new ArrayList<BuildResult>(results.size());
		for (Object res : results) {
			Map resMap = (Map) res;
			BuildResult buildResult = new BuildResult();
			buildResult.setProject(project);
			buildResult.setBuildNumber(Integer.valueOf(String.valueOf(resMap.get("buildNumber"))));
			buildResult.setState(Integer.valueOf(String.valueOf(resMap.get("state"))));
			result.add(buildResult);
		}
		return result;
	}
}
